import api from './request.js'
import apiHttp from './http.apis.js'

/**
 * wx-api 调用
 */
exports.install = function(Vue, options) {
	
	/**
	 * 检验是否授权
	 * @param {Object} scopeAuthName  授权的类别，例如 scope.userInfo，scope.location
	 * @return {type} return true 已授权，否则未授权
	 */
	Vue.prototype.$checkAuth = function(scopeAuthName) {
		// #ifdef MP-WEIXIN
		console.log("进来$checkAuth-----",scopeAuthName) 
		let flag = false
		uni.getSetting({
			success: (res) => {
				console.log("授权信息明细-",res) 
				// 查看是否授权  
			    if (res.authSetting['scope.userInfo']) {
					console.log("进来-授权")
					flag = true
				} else {
					// 没有授权  显示授权 button  
					console.log("进来-没有授权")
					flag = false
				}
			}
		});
		return flag
		// #endif
	}
  
  

    
	/**
	 * 授权并登陆
	 */
	Vue.prototype.$authLogin = function(userInfo = null) {
		// #ifdef MP-WEIXIN
		console.log("userInfo--",userInfo)
		let infoRes = userInfo
		// 获取用户信息
		uni.getUserInfo({
			provider: 'weixin',
			success: (infoResult) => {
				console.log('infoRes-base-：' ,infoResult);
				console.log('用户昵称为-base-：' + infoResult.userInfo.nickName);
				infoRes = infoResult
				
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						console.log("loginRes--base--",loginRes)
						
						apiHttp.login({
							code: loginRes.code,  
							user_info: infoRes.rawData,
							encrypted_data: infoRes.encryptedData,
							iv: infoRes.iv,
							signature: infoRes.signature,
						})  
						.then(res => {
							console.log("/login/login-res-base-",res)
							console.log("/login/login-res-base--infoRes.detail.userInfo--",infoRes.userInfo)
							
							this.$store.commit("setUserInfo",infoRes.userInfo)
							uni.setStorageSync("userInfo",infoRes.userInfo)
							uni.setStorageSync("token",res.data.token)
						})
						.catch(err => {
							console.log("/user/login-error",err)
						})
						
					}
				});
			}
		});
		// #endif
	}
  
	/**
	 * 登陆
	 * 
	 * @param {Object} infoRes   getUserInfo的信息
	 */
	Vue.prototype.$loginBase = function(infoRes) {
		// #ifdef MP-WEIXIN
		uni.login({
			provider: 'weixin',
			success: (loginRes) => {
				console.log("loginRes--base--",loginRes)
				
				apiHttp.login({
					code: loginRes.code,  
					user_info: infoRes.detail.rawData,
					encrypted_data: infoRes.detail.encryptedData,
					iv: infoRes.detail.iv,
					signature: infoRes.detail.signature,
				})  
				.then(res =>{
					console.log("/user/login-res",res)
					
					this.$store.commit("setUserInfo",infoRes.detail.userInfo) 
					
					uni.setStorageSync("userInfo",infoRes.detail.userInfo)
					uni.setStorageSync("token",res.data.token)
					
				})
				.catch(err =>{
					console.log("/user/login-error",err)
				})
				
			}
		});
		// #endif
	}
	
	
	/**
	 * 
	 * 授权
	 * @param {Object} infoRes
	 */
	Vue.prototype.$login = function(infoRes) {
		// #ifdef MP-WEIXIN
		uni.authorize({
			scope: 'scope.userInfo',  
			success: (res) => {
				console.log('res-----',uni.getStorageSync("userInfo"))
			},
			fail: (err) => {
				this.$u.toast("授权失败")
				console.log('授权失败,uni.authorize-err',err)
			}
		})
		// #endif
	}
	
	/**
	 * 分享
	 */
	Vue.prototype.$share = function() {
		// #ifdef MP-WEIXIN
			uni.showShareMenu({
			withShareTicket:true,
			menus:['shareAppMessage','shareTimeline']
		})
		// #endif
	}
	
	/**
	 * 获取当前位置
	 */
	Vue.prototype.$getAddress = function() {
		// #ifdef MP-WEIXIN
		uni.getLocation({
			type: 'gcj02',  // wgs84   gcj02微信用
			success: (res) => {
				console.log('当前位置的经度：' + res.longitude);
				console.log('当前位置的纬度：' + res.latitude);
				
				uni.setStorageSync('now-jd',res.longitude);
				uni.setStorageSync('now-wd',res.latitude);
			},
			fail: (err) => {
				console.log('getLocation：', JSON.stringify(err));
			}
		});
		// #endif
	}
	
	/**
	 * 选择当前位置
	 */
	Vue.prototype.$chooseLocation = function() {
		// #ifdef MP-WEIXIN
		uni.chooseLocation({
		    success: function (res) {
		        console.log('位置名称：' + res.name);
		        console.log('详细地址：' + res.address);
		        console.log('纬度：' + res.latitude);
		        console.log('经度：' + res.longitude);
		    }
		});
		// #endif
	}
	
	

	
	
	
	
	
}
